
var jumpTableIndex = 0;

const jp_r16 = 0xc3;

function disassembleJumpTable(bank, startAddress, endAddress, selectedBank, functionNames) {
  jumpTableIndex += 1;

  registerJumpTable(bank, startAddress, endAddress);

  function bankSelector(index) {
    if (typeof selectedBank === 'number') {
      return selectedBank;
    }
    if (selectedBank.constructor == Object) {
      return selectedBank[index];
    }
    return null;
  }

  const data = getROMData(bank, startAddress, endAddress);
  for (let jumpFromAddress = startAddress; jumpFromAddress < endAddress; jumpFromAddress += 2) {
    let lowByte = data[jumpFromAddress - startAddress];
    let highByte = data[jumpFromAddress - startAddress + 1];
    let jumpToAddress = (highByte << 8) | lowByte;
    if (jumpToAddress >= 0x8000) {
      continue;
    }
    let index = parseInt((jumpFromAddress - startAddress) / 2);
    let selectedBank = 0;
    if (jumpToAddress >= 0x4000) {
      selectedBank = bankSelector(index);
      if (typeof selectedBank !== 'number') {
        continue;
      }
      registerBankChange(selectedBank, jumpFromAddress, bank);
    }
    if (selectedBank == 0 && jumpToAddress >= 0x4000) {
      continue; // Don't disassemble if we're not confident what the bank is.
    }
    let jumpName;
    if (functionNames && typeof functionNames[index] === 'string') {
      jumpName = "JumpTable_" + functionNames[index];
    } else {
      jumpName = "JumpTable_" + hex16(jumpToAddress) + "_" + hex8(selectedBank);
    }
    registerTransferOfControl(selectedBank, jumpToAddress, bank, jumpFromAddress, jp_r16);
    registerFunction(selectedBank, jumpToAddress, jumpName);
  }
}

function disassemblyWillStart() {
  jumpTableIndex = 0;
  disassembleJumpTable(0x00, 0x04b3, 0x04F5, 0x00);
  disassembleJumpTable(0x00, 0x1b6e, 0x1b90, 0x00);
  disassembleJumpTable(0x00, 0x0ad2, 0x0aea, 0x00);
  disassembleJumpTable(0x00, 0x215f, 0x217d, 0x00);
  disassembleJumpTable(0x00, 0x0c82, 0x0C8C, 0x01);
  disassembleJumpTable(0x00, 0x0d33, 0x0d49, 0x03);
  disassembleJumpTable(0x00, 0x30fb, 0x310d, 0x00);
  disassembleJumpTable(0x00, 0x3114, 0x3138, 0x00);
  disassembleJumpTable(0x00, 0x392b, 0x393d, 0x03);
  disassembleJumpTable(0x01, 0x4187, 0x4191, 0x01);
  disassembleJumpTable(0x01, 0x4322, 0x4332, 0x01);
  disassembleJumpTable(0x02, 0x5378, 0x5392, 0x02);
  disassembleJumpTable(0x02, 0x5b2f, 0x5b3f, 0x02);
  disassembleJumpTable(0x02, 0x5d45, 0x5d63, 0x02);
  disassembleJumpTable(0x02, 0x6b4e, 0x6b56, 0x02);
  disassembleJumpTable(0x02, 0x6b74, 0x6b7c, 0x02);
  disassembleJumpTable(0x02, 0x6b9a, 0x6ba2, 0x02);
  disassembleJumpTable(0x02, 0x6c1f, 0x6c25, 0x02);
  disassembleJumpTable(0x02, 0x7c53, 0x7c5d, 0x02);
  disassembleJumpTable(0x03, 0x4976, 0x4B48, 0x03);
  disassembleJumpTable(0x03, 0x5aa6, 0x5ab8, 0x03);
  disassembleJumpTable(0x03, 0x5bf5, 0x5bfd, 0x03);
  disassembleJumpTable(0x03, 0x5de0, 0x5de6, 0x03);
  disassembleJumpTable(0x03, 0x5e43, 0x5e53, 0x03);
  disassembleJumpTable(0x03, 0x5ef7, 0x5f01, 0x03);
  disassembleJumpTable(0x03, 0x6353, 0x6375, 0x03);
  disassembleJumpTable(0x03, 0x700b, 0x7017, 0x03);
  disassembleJumpTable(0x04, 0x4015, 0x401f, 0x04);
  disassembleJumpTable(0x04, 0x4091, 0x4099, 0x04);
  disassembleJumpTable(0x04, 0x42e5, 0x42eb, 0x04);
  disassembleJumpTable(0x04, 0x4328, 0x4334, 0x04);
  disassembleJumpTable(0x04, 0x49d0, 0x49d4, 0x04);
  disassembleJumpTable(0x04, 0x49dd, 0x49e5, 0x04);
  disassembleJumpTable(0x04, 0x4b52, 0x4b56, 0x04);
  disassembleJumpTable(0x04, 0x4e0d, 0x4e13, 0x04);
  disassembleJumpTable(0x04, 0x4e8c, 0x4e94, 0x04);
  disassembleJumpTable(0x04, 0x5078, 0x5080, 0x04);
  disassembleJumpTable(0x04, 0x50a1, 0x50a7, 0x04);
  disassembleJumpTable(0x04, 0x512f, 0x5135, 0x04);
  disassembleJumpTable(0x04, 0x553f, 0x5545, 0x04);
  disassembleJumpTable(0x04, 0x55b0, 0x55b6, 0x04);
  disassembleJumpTable(0x04, 0x56bd, 0x56C5, 0x04);
  disassembleJumpTable(0x04, 0x5E23, 0x5E29, 0x04);
  disassembleJumpTable(0x04, 0x5FCF, 0x5FD5, 0x04);
  disassembleJumpTable(0x04, 0x6081, 0x6089, 0x04);
  disassembleJumpTable(0x04, 0x6802, 0x6806, 0x04);
  disassembleJumpTable(0x04, 0x69B0, 0x69B6, 0x04);
  disassembleJumpTable(0x04, 0x6EB6, 0x6ED0, 0x04);
  disassembleJumpTable(0x04, 0x747B, 0x7487, 0x04);
  disassembleJumpTable(0x04, 0x76B4, 0x76c2, 0x04);
  disassembleJumpTable(0x04, 0x79E5, 0x79F7, 0x04);
  disassembleJumpTable(0x04, 0x7CCE, 0x7CD2, 0x04);
  disassembleJumpTable(0x04, 0x7DA6, 0x7DAC, 0x04);
  disassembleJumpTable(0x04, 0x7E82, 0x7E8A, 0x04);
  disassembleJumpTable(0x05, 0x40AE, 0x40B2, 0x05);
  disassembleJumpTable(0x05, 0x4169, 0x4173, 0x05);
  disassembleJumpTable(0x05, 0x461E, 0x4626, 0x05);
  disassembleJumpTable(0x05, 0x47F9, 0x4803, 0x05);
  disassembleJumpTable(0x05, 0x4988, 0x4990, 0x05);
  disassembleJumpTable(0x05, 0x4BFF, 0x4C09, 0x05);
  disassembleJumpTable(0x05, 0x4EB7, 0x4EC3, 0x05);
  disassembleJumpTable(0x05, 0x51A4, 0x51AA, 0x05);
  disassembleJumpTable(0x05, 0x52C4, 0x52CA, 0x05);
  disassembleJumpTable(0x05, 0x5395, 0x539B, 0x05);
  disassembleJumpTable(0x05, 0x54B0, 0x54B8, 0x05);
  disassembleJumpTable(0x05, 0x54CD, 0x54D1, 0x05);
  disassembleJumpTable(0x05, 0x5625, 0x562B, 0x05);
  disassembleJumpTable(0x05, 0x59C1, 0x59C9, 0x05);
  disassembleJumpTable(0x05, 0x59CC, 0x59D6, 0x05);
  disassembleJumpTable(0x05, 0x5AFA, 0x5B18, 0x05);
  disassembleJumpTable(0x05, 0x5C64, 0x5C6E, 0x05);
  disassembleJumpTable(0x05, 0x61BD, 0x61C1, 0x05);
  disassembleJumpTable(0x05, 0x6224, 0x6228, 0x05);
  disassembleJumpTable(0x05, 0x628B, 0x628F, 0x05);
  disassembleJumpTable(0x05, 0x62CD, 0x62D9, 0x05);
  disassembleJumpTable(0x05, 0x6C5D, 0x6C65, 0x05);
  disassembleJumpTable(0x05, 0x7210, 0x721A, 0x05);
  disassembleJumpTable(0x05, 0x6701, 0x6705, 0x05);
  disassembleJumpTable(0x05, 0x67E9, 0x67EF, 0x05);
  disassembleJumpTable(0x05, 0x6C50, 0x6C56, 0x05);
  disassembleJumpTable(0x05, 0x7B4E, 0x7B56, 0x05);
  disassembleJumpTable(0x05, 0x7D93, 0x7D99, 0x05);
  disassembleJumpTable(0x06, 0x404C, 0x4056, 0x06);
  disassembleJumpTable(0x06, 0x4172, 0x417C, 0x06);
  disassembleJumpTable(0x06, 0x426E, 0x4278, 0x06);
  disassembleJumpTable(0x06, 0x4585, 0x458F, 0x06);
  disassembleJumpTable(0x06, 0x47F8, 0x4802, 0x06);
  disassembleJumpTable(0x06, 0x4964, 0x496A, 0x06);
  disassembleJumpTable(0x06, 0x4AE0, 0x4AE8, 0x06);
  disassembleJumpTable(0x06, 0x4BFC, 0x4C00, 0x06);
  disassembleJumpTable(0x06, 0x5143, 0x514F, 0x06);
  disassembleJumpTable(0x06, 0x53D1, 0x53D9, 0x06);
  disassembleJumpTable(0x06, 0x54DB, 0x54EB, 0x06);
  disassembleJumpTable(0x06, 0x56EE, 0x56F6, 0x06);
  disassembleJumpTable(0x06, 0x5824, 0x5834, 0x06);
  disassembleJumpTable(0x06, 0x5B71, 0x5B79, 0x06);
  disassembleJumpTable(0x06, 0x5DA6, 0x5DAE, 0x06);
  disassembleJumpTable(0x06, 0x5F6A, 0x5F74, 0x06);
  disassembleJumpTable(0x06, 0x6117, 0x611D, 0x06);
  disassembleJumpTable(0x06, 0x61DA, 0x61DE, 0x06);
  disassembleJumpTable(0x06, 0x62F0, 0x62F6, 0x06);
  disassembleJumpTable(0x06, 0x6757, 0x675B, 0x06);
  disassembleJumpTable(0x06, 0x68D7, 0x68E1, 0x06);
  disassembleJumpTable(0x06, 0x6C81, 0x6C85, 0x06);
  disassembleJumpTable(0x06, 0x6D17, 0x6D1F, 0x06);
  disassembleJumpTable(0x06, 0x6F7A, 0x6F80, 0x06);
  disassembleJumpTable(0x06, 0x7069, 0x706F, 0x06);
  disassembleJumpTable(0x06, 0x726A, 0x7270, 0x06);
  disassembleJumpTable(0x06, 0x7383, 0x7389, 0x06);
  disassembleJumpTable(0x06, 0x74C5, 0x74C9, 0x06);
  disassembleJumpTable(0x06, 0x7574, 0x757A, 0x06);
  disassembleJumpTable(0x06, 0x7621, 0x7629, 0x06);
  disassembleJumpTable(0x06, 0x773F, 0x7747, 0x06);
  disassembleJumpTable(0x06, 0x7AC8, 0x7ACC, 0x06);
  disassembleJumpTable(0x06, 0x7C7F, 0x7C8B, 0x06);
  disassembleJumpTable(0x06, 0x7DBF, 0x7DC9, 0x06);
  disassembleJumpTable(0x06, 0x7EC7, 0x7ECB, 0x06);
  disassembleJumpTable(0x07, 0x40B5, 0x40B7, 0x07);
  disassembleJumpTable(0x07, 0x42CD, 0x42CF, 0x07);
  disassembleJumpTable(0x07, 0x4545, 0x4547, 0x07);
  disassembleJumpTable(0x07, 0x4715, 0x4717, 0x07);
  disassembleJumpTable(0x07, 0x489D, 0x489F, 0x07);
  disassembleJumpTable(0x07, 0x49FE, 0x4A00, 0x07);
  disassembleJumpTable(0x07, 0x4AB3, 0x4AB5, 0x07);
  disassembleJumpTable(0x07, 0x4B8E, 0x4B90, 0x07);
  disassembleJumpTable(0x07, 0x4CDF, 0x4CE1, 0x07);
  disassembleJumpTable(0x07, 0x4F1A, 0x4F1C, 0x07);
  disassembleJumpTable(0x07, 0x5124, 0x5126, 0x07);
  disassembleJumpTable(0x07, 0x5627, 0x5629, 0x07);
  disassembleJumpTable(0x07, 0x59B4, 0x59B6, 0x07);
  disassembleJumpTable(0x07, 0x5B95, 0x5B97, 0x07);
  disassembleJumpTable(0x07, 0x5E3C, 0x5E3E, 0x07);
  disassembleJumpTable(0x07, 0x6221, 0x6223, 0x07);
  disassembleJumpTable(0x07, 0x640F, 0x6411, 0x07);
  disassembleJumpTable(0x07, 0x64BC, 0x64BE, 0x07);
  disassembleJumpTable(0x07, 0x654C, 0x654E, 0x07);
  disassembleJumpTable(0x07, 0x66A4, 0x66A6, 0x07);
  disassembleJumpTable(0x07, 0x6862, 0x6864, 0x07);
  disassembleJumpTable(0x07, 0x69CF, 0x69D1, 0x07);
  disassembleJumpTable(0x07, 0x71EB, 0x71ED, 0x07);
  disassembleJumpTable(0x07, 0x731A, 0x731C, 0x07);
  disassembleJumpTable(0x07, 0x747F, 0x7481, 0x07);
  disassembleJumpTable(0x07, 0x7547, 0x7549, 0x07);
  disassembleJumpTable(0x07, 0x772F, 0x7731, 0x07);
  disassembleJumpTable(0x07, 0x788F, 0x7891, 0x07);
  disassembleJumpTable(0x07, 0x7D88, 0x7D8A, 0x07);
  disassembleJumpTable(0x14, 0x5001, 0x5003, 0x14);
  disassembleJumpTable(0x15, 0x40A7, 0x40A9, 0x15);
  disassembleJumpTable(0x15, 0x410C, 0x410E, 0x15);
  disassembleJumpTable(0x15, 0x41D6, 0x41D8, 0x15);
  disassembleJumpTable(0x15, 0x4249, 0x424B, 0x15);
  disassembleJumpTable(0x15, 0x42BC, 0x42BE, 0x15);
  disassembleJumpTable(0x15, 0x44E8, 0x44EA, 0x15);
  disassembleJumpTable(0x15, 0x45F1, 0x45F3, 0x15);
  disassembleJumpTable(0x15, 0x4728, 0x472A, 0x15);
  disassembleJumpTable(0x15, 0x4F6B, 0x4F6D, 0x15);
  disassembleJumpTable(0x15, 0x50BB, 0x50BD, 0x15);
  disassembleJumpTable(0x15, 0x7701, 0x7703, 0x15);
  disassembleJumpTable(0x15, 0x78E1, 0x78E3, 0x15);
  disassembleJumpTable(0x15, 0x7C41, 0x7C43, 0x15);
  disassembleJumpTable(0x17, 0x488B, 0x488D, 0x17);
  disassembleJumpTable(0x17, 0x754D, 0x754F, 0x17);
  disassembleJumpTable(0x18, 0x401F, 0x4021, 0x18);
  disassembleJumpTable(0x18, 0x4390, 0x4392, 0x18);
  disassembleJumpTable(0x18, 0x4547, 0x4549, 0x18);
  disassembleJumpTable(0x18, 0x4962, 0x4964, 0x18);
  disassembleJumpTable(0x18, 0x4A04, 0x4A06, 0x18);
  disassembleJumpTable(0x18, 0x4BA1, 0x4BA3, 0x18);
  disassembleJumpTable(0x18, 0x4BFC, 0x4BFE, 0x18);
  disassembleJumpTable(0x18, 0x4CF7, 0x4CF9, 0x18);
  disassembleJumpTable(0x18, 0x4DE8, 0x4DEA, 0x18);
  disassembleJumpTable(0x18, 0x4E56, 0x4E58, 0x18);
  disassembleJumpTable(0x18, 0x52CC, 0x52CE, 0x18);
  disassembleJumpTable(0x18, 0x552B, 0x552D, 0x18);
  disassembleJumpTable(0x18, 0x58AB, 0x58AD, 0x18);
  disassembleJumpTable(0x18, 0x5B93, 0x5B95, 0x18);
  disassembleJumpTable(0x18, 0x5E21, 0x5E23, 0x18);
  disassembleJumpTable(0x18, 0x5F02, 0x5F04, 0x18);
  disassembleJumpTable(0x18, 0x60E9, 0x60EB, 0x18);
  disassembleJumpTable(0x18, 0x61A3, 0x61A5, 0x18);
  disassembleJumpTable(0x18, 0x63F2, 0x63F4, 0x18);
  disassembleJumpTable(0x18, 0x65B3, 0x65B5, 0x18);
  disassembleJumpTable(0x18, 0x6A65, 0x6A67, 0x18);
  disassembleJumpTable(0x18, 0x6F70, 0x6F72, 0x18);
  disassembleJumpTable(0x18, 0x6FFD, 0x6FFF, 0x18);
  disassembleJumpTable(0x18, 0x7175, 0x7177, 0x18);
  disassembleJumpTable(0x18, 0x735D, 0x735F, 0x18);
  disassembleJumpTable(0x18, 0x773D, 0x773F, 0x18);
  disassembleJumpTable(0x18, 0x7828, 0x782A, 0x18);
  disassembleJumpTable(0x18, 0x7E82, 0x7E84, 0x18);
  disassembleJumpTable(0x19, 0x406A, 0x406C, 0x19);
  disassembleJumpTable(0x19, 0x428F, 0x4291, 0x19);
  disassembleJumpTable(0x19, 0x4495, 0x4497, 0x19);
  disassembleJumpTable(0x19, 0x461C, 0x461E, 0x19);
  disassembleJumpTable(0x19, 0x4942, 0x4944, 0x19);
  disassembleJumpTable(0x19, 0x4A33, 0x4A35, 0x19);
  disassembleJumpTable(0x19, 0x4CB3, 0x4CB5, 0x19);
  disassembleJumpTable(0x19, 0x4D7B, 0x4D7D, 0x19);
  disassembleJumpTable(0x19, 0x51A9, 0x51AB, 0x19);
  disassembleJumpTable(0x19, 0x5350, 0x5352, 0x19);
  disassembleJumpTable(0x19, 0x54DD, 0x54DF, 0x19);
  disassembleJumpTable(0x19, 0x5609, 0x560B, 0x19);
  disassembleJumpTable(0x19, 0x5719, 0x571B, 0x19);
  disassembleJumpTable(0x19, 0x5823, 0x5825, 0x19);
  disassembleJumpTable(0x19, 0x5B29, 0x5B2B, 0x19);
  disassembleJumpTable(0x19, 0x5CB6, 0x5CB8, 0x19);
  disassembleJumpTable(0x19, 0x5E07, 0x5E09, 0x19);
  disassembleJumpTable(0x19, 0x5F1E, 0x5F20, 0x19);
  disassembleJumpTable(0x19, 0x614A, 0x614C, 0x19);
  disassembleJumpTable(0x19, 0x65E2, 0x65E4, 0x19);
  disassembleJumpTable(0x19, 0x682E, 0x6830, 0x19);
  disassembleJumpTable(0x19, 0x68B7, 0x68B9, 0x19);
  disassembleJumpTable(0x19, 0x69CC, 0x69CE, 0x19);
  disassembleJumpTable(0x19, 0x6BF5, 0x6BF7, 0x19);
  disassembleJumpTable(0x19, 0x6E51, 0x6E53, 0x19);
  disassembleJumpTable(0x19, 0x70CD, 0x70CF, 0x19);
  disassembleJumpTable(0x19, 0x76A8, 0x76AA, 0x19);
}
